package cb.com.etcdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cb.com.etcdemo.API.API;
import cb.com.etcdemo.bean.DeviceStateOnLine;
import cb.com.etcdemo.bean.JieGuo;
import cb.com.etcdemo.http.Okhttp;
import cb.com.etcdemo.util.JSONUtils;
import cb.com.etcdemo.util.SpfUtils;
import cb.com.etcdemohgz.R;

public class WeiXiuFanKuiActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.yuyue)
    TextView yuyue;
    @BindView(R.id.shoufei_name)
    TextView shoufeiName;
    @BindView(R.id.shijian_lay)
    LinearLayout shijianLay;
    @BindView(R.id.chedao)
    TextView chedao;
    @BindView(R.id.chedao_id)
    TextView chedaoId;
    @BindView(R.id.zhuji)
    TextView zhuji;
    @BindView(R.id.zhuji_banben)
    TextView zhujiBanben;
    @BindView(R.id.fg)
    TextView fg;
    @BindView(R.id.foug_banben)
    TextView fougBanben;
    @BindView(R.id.chedao_bianhao)
    EditText chedaoBianhao;
    @BindView(R.id.congji_shebeihao)
    EditText congjiShebeihao;
    @BindView(R.id.chuankou_hao)
    EditText chuankouHao;
    @BindView(R.id.weixiu_jieguo)
    EditText weixiuJieguo;
    @BindView(R.id.weixu_beizhu)
    EditText weixuBeizhu;
    @BindView(R.id.weixiutianme)
    TextView weixiutianme;
    @BindView(R.id.activity_seetting)
    LinearLayout activitySeetting;
    @BindView(R.id.zhuji_shebeihao)
    TextView zhujiShebeihao;
    DeviceStateOnLine.ListBean info;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xiu_fan_kui);
        Intent intent = getIntent();
         info = (DeviceStateOnLine.ListBean) intent.getSerializableExtra("listBean");
        ButterKnife.bind(this);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        Date curr = c2.getTime();
        String currs = format.format(curr);
        Log.e("88882", currs);
        shoufeiName.setText(info.getStationName() == null ? "暂无" : "" + info.getStationName());
        fougBanben.setText(info.getFgBoxVersion() == null ? "暂无" : "" + info.getFgBoxVersion());
        chedaoId.setText(info.getVehicleNumOri() == null ? "暂无" : "" + info.getVehicleNumOri());
        zhujiBanben.setText(info.getCurrentVersion() == null ? "暂无" : "" + info.getCurrentVersion());

        zhujiShebeihao.setText(info.getDeviceIp() == null ? "暂无" : "" + info.getDeviceId());
        weixiutianme.setText(currs);
//        holder.tv_offLineMinute.setText(info.getOffLineMinute() == null ? "离线时长(分)：暂无" : "离线时长(分)：" + info.getOffLineMinute());
//        holder.tv_Lanenumber.setText(info.getVehicleNum() == null ? "车道编号：暂无" : "车道编号：" + info.getVehicleNum());
//
//        holder.tv_paynet.setText(info.getPaynet() == null ? "支付网络状态：暂无" : "支付网络状态：" + info.getPaynet());
//        holder.tv_sernet.setText(info.getSerial() == null ? "通信串口状态：暂无" : "通信串口状态：" + info.getSerial());
//        holder.tv_sla.setText(info.getSlavemac() == null ? "从机通信状态：暂无" : "从机通信状态：" + info.getSlavemac());
//        holder.tv_ses.setText(info.getSestatus() == null ? "SE状态：暂无" : "SE状态：" + info.getSestatus());


    }
    KProgressHUD dialog;

    private void showdialog() {
        if (null == dialog) {
            dialog = KProgressHUD.create(WeiXiuFanKuiActivity.this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel("正在加载中，请稍候")
                    .setCancellable(true)
                    .setAnimationSpeed(2)
                    .setDimAmount(0.5f);
        }
        dialog.show();
    }
    @OnClick({R.id.img_back, R.id.textView8})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.textView8:
                String stationName = ""+info.getStationName();


                String  vehicleId =""+ info.getVehicleNumOri();
                 String hostCode= ""+info.getDeviceId();
                String hostVersion= ""+info.getCurrentVersion();
                if (TextUtils.isEmpty(chedaoBianhao.getText().toString())) {
                    Toast.makeText(this, "请输入车道编号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(congjiShebeihao.getText().toString())) {
                    Toast.makeText(this, "请输入从机设备号", Toast.LENGTH_SHORT).show();
                 return;
                }

                if (TextUtils.isEmpty(chuankouHao.getText().toString())) {
                    Toast.makeText(this, "请输入串口号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(weixiuJieguo.getText().toString())) {
                  Toast.makeText(this, "请输入维修结果", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(weixuBeizhu.getText().toString())) {
//                    Toast.makeText(this, "请输入维修备注", Toast.LENGTH_SHORT).show();
//                    return;
                }

                String vehicleCode =""+  chedaoBianhao.getText().toString();
                String slaveCode=congjiShebeihao.getText().toString();
               String slaveVersion= ""+info.getSlavemac();
                String serialVersion=chuankouHao.getText().toString();
                 String fgboxVersion=""+info.getFgBoxVersion();
//
                 String repairResult=weixiuJieguo.getText().toString();
                String repariRemark=weixuBeizhu.getText().toString();
                String name = (String) SpfUtils.get(WeiXiuFanKuiActivity.this, "name", "");
                String opUser=name;
                 String opDate=weixiutianme.getText().toString();
                showdialog();
                API.Dijiao(stationName,
                        vehicleCode,
                        vehicleId,
                        hostCode,
                        hostVersion,
                        slaveCode,
                        slaveVersion,
                        serialVersion,
                        fgboxVersion,
                        repairResult,
                        repariRemark,
                        opUser,
                        opDate, new Okhttp.Objectcallback() {
                            @Override
                            public void onsuccess(String st) {
                                if (dialog!=null) {
                                    dialog.dismiss();
                                }
                                Log.e("888", "xx"+st);
                                JieGuo jieGuo = JSONUtils.parseJSON(st, JieGuo.class);
                                if (jieGuo.getCode().equals("0")) {
                                    String msg = jieGuo.getMsg();
                                    Toast.makeText(WeiXiuFanKuiActivity.this, ""+msg, Toast.LENGTH_SHORT).show();
finish();
                                } else {
                                    String msg = jieGuo.getMsg();
                                    Toast.makeText(WeiXiuFanKuiActivity.this, ""+msg, Toast.LENGTH_SHORT).show();
                                }

                            }

                            @Override
                            public void onFalia(int code, String errst) {
                                Log.e("888", "xx"+errst);
                                if (dialog!=null) {
                                    dialog.dismiss();
                                }
                                Toast.makeText(WeiXiuFanKuiActivity.this, "网络错误："+errst, Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
        }
    }
}
